//
//  ST_JVAlbumListController.swift
//  JVPhotoPicker-Example
//
//  Created by mac on 2019/11/12.
//  Copyright © 2019 mac. All rights reserved.
//

import UIKit
import SnapKit
import TZImagePickerController
import SwifterSwift
import SwiftyAttributes

class ST_JVAlbumListController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        TZImageManager().getAllAlbums(true, allowPickingImage: true, needFetchAssets: false) { (list) in
            
            if let array = list{
                
                self.data_array = array
                
                self.collectionView.reloadData()
            }
        }
        
    }
    
    
//    MARK: - Properties
    
    fileprivate lazy var collectionView: UICollectionView = {
            
        let layout = UICollectionViewFlowLayout()
//        layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
        layout.itemSize = CGSize.init(width: ST_JVScreenWidth, height: JVAlbumCell.height)
        //        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5

        let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
        
        collection.backgroundColor = Color.white
        
        collection.register(cellWithClass: JVAlbumCell.self)
        //        collection.alwaysBounceVertical = true
        collection.delegate = self
        
        collection.dataSource = self
        
        collection.alwaysBounceVertical = true
        
        return collection
    }()
    
    fileprivate var data_array = [TZAlbumModel]()
    
    var selectedAlbumClosure:((TZAlbumModel)->())?
//    MARK: - Event
}


extension ST_JVAlbumListController:UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data_array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withClass: JVAlbumCell.self, for: indexPath)
        
        cell.model = data_array[indexPath.row]
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        
        let model = data_array[indexPath.row]
        
        
        selectedAlbumClosure?(model)
        
        self.view.removeFromSuperview()
        
        self.removeFromParent()
    }
    
    
    
    
    
}



//MARK: - Cell
class JVAlbumCell: UICollectionViewCell {
    
    static let height = 60.cgFloat
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(50)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15)
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(icon_image_view.snp.right).offset(10)
            make.centerY.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //    MARK: - Properties
    lazy var icon_image_view:UIImageView = {
       let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.clipsToBounds = true
        return control
    }()
    
    lazy var titleLabel:UILabel = {
       let control = UILabel()
        return control
    }()
    
    
    //    MARK: - Bind Model
    var model = TZAlbumModel(){
        didSet{
            
            let str1 = model.name.withAttributes([Attribute.textColor(UIColor.init(hex: 0x333333)!),Attribute.font(Font.systemFont(ofSize: 16, weight: UIFont.Weight.semibold))])
            
            let str2 = ("（"+model.count.string+"）").withAttributes([Attribute.textColor(UIColor.init(hex: 0x999999)!),Attribute.font(Font.systemFont(ofSize: 16, weight: UIFont.Weight.regular))])
            
            str1.append(str2)
            
            titleLabel.attributedText = str1
            
            TZImageManager().getPostImage(with: model) { (image) in
                guard let img = image else{
                    return
                }
                self.icon_image_view.image = img
            }
        }
    }
    
}
